﻿using ManagementApi.Models;
using PmSoft;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ManagementApi
{
    /// <summary>
    /// IPagedList 扩展方法
    /// </summary>
    public static class IPagedListExtensions
    {
        /// <summary>
        /// 转换为分页数据传输对象
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="sources"></param>
        /// <param name="convert"></param>
        /// <returns></returns>
        public static PageDto<TDestination> AsPageDto<TSource, TDestination>(this IPagedList<TSource> sources, Func<TSource, TDestination> convert)
        {
            return new PageDto<TDestination>(sources.Select(s => convert(s)), sources.PageNum, sources.PageSize, sources.Total, sources.Duration);
        }

        public static PageDto<TDestination> AsPageDtos<TSource, TDestination>(this IPagedList<TSource> sources, Func<IEnumerable<TSource>, IEnumerable<TDestination>> convert)
        {
            return new PageDto<TDestination>(convert(sources), sources.PageNum, sources.PageSize, sources.Total, sources.Duration);
        }

    }
}